package smlab.media.FLVPlayer.view
{
	import flash.display.DisplayObjectContainer;
	import flash.events.EventDispatcher;
	
	import mx.core.IContainer;
	
	import smlab.interfaces.IContainer_SM;
	import smlab.media.FLVPlayer.CoreGlobal;
	import smlab.media.FLVPlayer.events.StateEvent;
	import smlab.media.FLVPlayer.view.state.ADState;
	import smlab.media.FLVPlayer.view.state.LoadingState;
	import smlab.media.FLVPlayer.view.state.PlayerState;
	import smlab.view.state.Container_SM;
	
	/**
	 * 所有场景容器
	 * @author simon
	 * 
	 */
	public class StateContainer extends Container_SM
	{
		public static const LOADING_STATE:String="loading_state";
		public static const AD_STATE:String="ad_state";
		public static const PLAYER_STATE:String="player_state";
		
		/**
		 * 加载场景
		 */
		private var loadingState:LoadingState;
		
		/**
		 * 广告场景
		 */
		private var adState:ADState;
		
		/**
		 * 播放器场景
		 */
		private var playerState:PlayerState;
		
		private static var _instance:StateContainer;
		
		public function StateContainer()
		{
			super();
			
			if(_instance){
				throw new Error("StateContainer singleton Error");
			}
			
			initialize();
		}
		
		static public function getInstance():StateContainer{
			if(!_instance){
				_instance=new StateContainer;
			}
			return _instance;
		}
		
		override public function initialize():void
		{
			loadingState=new LoadingState;
			loadingState.stateName=LOADING_STATE;
			CoreGlobal.states[LOADING_STATE]=loadingState;
			addChild(loadingState);
			
			adState=new ADState;
			adState.stateName=AD_STATE;
			CoreGlobal.states[AD_STATE]=adState;
			addChild(adState);
			
			playerState=new PlayerState;
			playerState.stateName=PLAYER_STATE;
			CoreGlobal.states[PLAYER_STATE]=playerState;
			addChild(playerState);
			
			CoreGlobal.stateEventDispatcher=new EventDispatcher;
			CoreGlobal.stateEventDispatcher.addEventListener(StateEvent.SWITCH_TO_LOADING_STATE,stateEventHandler);
			CoreGlobal.stateEventDispatcher.addEventListener(StateEvent.SWITCH_TO_AD_STATE,stateEventHandler);
			CoreGlobal.stateEventDispatcher.addEventListener(StateEvent.SWITCH_TO_PLAYER_STATE,stateEventHandler);
			CoreGlobal.stateEventDispatcher.addEventListener(StateEvent.STATE_CHANGE,onStateChange);
			CoreGlobal.stateEventDispatcher.addEventListener(StateEvent.STATE_CHANGED,onStateChanged);
			
			this.initialized=true;
			dispatchEvent(new StateEvent(StateEvent.STATE_INITIALIZED));
			start();
		}
		
		/**
		 * 执行场景切换(必须用事件来通知场景切换，保证正常切换)
		 * @param e
		 * 
		 */
		private function stateEventHandler(e:StateEvent):void
		{
			var currentState:IContainer_SM=CoreGlobal.states[CoreGlobal.currentStateName];
			var nextState:IContainer_SM;
			switch(e.type){
				case StateEvent.SWITCH_TO_LOADING_STATE:
					nextState=loadingState;
				break;
				case StateEvent.SWITCH_TO_AD_STATE:
					nextState=adState;
				break;
				case StateEvent.SWITCH_TO_PLAYER_STATE:
					nextState=playerState;
				break;
			}
			
			if(currentState!=nextState)
			{
				CoreGlobal.stateEventDispatcher.dispatchEvent(new StateEvent(StateEvent.STATE_CHANGE));
				currentState.sleep();
				
				if(nextState.initialized){
					CoreGlobal.currentStateName=nextState.stateName;
					CoreGlobal.stateEventDispatcher.dispatchEvent(new StateEvent(StateEvent.STATE_CHANGED));
					nextState.start();
				}else{
					(nextState as DisplayObjectContainer).addEventListener(StateEvent.STATE_INITIALIZED,onNextStateInizalized);
					nextState.initialize();
				}
			}
		}
		
		private function onNextStateInizalized(e:StateEvent):void{
			var nextState:IContainer_SM=e.target as IContainer_SM;
			CoreGlobal.currentStateName=nextState.stateName;
			CoreGlobal.stateEventDispatcher.dispatchEvent(new StateEvent(StateEvent.STATE_CHANGED));
			nextState.start();
		}
		
		override public function start():void{
			loadingState.addEventListener(StateEvent.STATE_INITIALIZED,onLoadingStateInizalized);
			loadingState.initialize();
		}
		
		override public function sleep():void{
			
		}
		
		/**
		 * loadingState 第一次运行
		 * @param e
		 * 
		 */
		private function onLoadingStateInizalized(e:StateEvent):void{
			CoreGlobal.currentStateName=loadingState.stateName;
			CoreGlobal.stateEventDispatcher.dispatchEvent(new StateEvent(StateEvent.STATE_CHANGED));
			loadingState.start();
		}
		
		/**
		 * 场景开始切换
		 * @param e
		 * 
		 */
		private function onStateChange(e:StateEvent):void
		{
			
		}
		
		/**
		 * 场景切换结束
		 * @param e
		 * 
		 */
		private function onStateChanged(e:StateEvent):void
		{
			
		}
		
	}
}